import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '../utils/token'
// import Layout from '../views/layout'
// import Login from '@/views/login'
// import Welcome from '../views/layout/welcome'
// import Chart from '../views/layout/chart'
// import User from '../views/layout/user'
// import Enterprise from '../views/layout/enterprise'
// import Question from '../views/layout/question'
// import Subject from '../views/layout/subject'
const Login = () => import('../views/login')
const Layout = () => import('@/views/layout')
const Welcome = () => import('@/views/layout/welcome')
const Chart = () => import('@/views/layout/chart')
const User = () => import('@/views/layout/user')
const Enterprise = () => import('@/views/layout/enterprise')
const Question = () => import('@/views/layout/question')
const Subject = () => import('@/views/layout/subject')

//测试用的组件
import Parent from '@/test/slot/parent'
import Slot from '@/test/slot2/slot'
import Slot3 from '@/test/slot3/slot'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录' }
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/layout',
    name: 'Layout',
    component: Layout,
    children: [
      {
        path: 'welcome',
        component: Welcome,
        meta: {
          title: '欢迎页面',
          icon: 'el-icon-pie-chart',
          path: '/layout/welcome',
          roles: ['超级管理员', '管理员', '老师', '学生']
        }
      },
      {
        path: 'chart',
        component: Chart,
        meta: {
          title: '数据列表',
          icon: 'el-icon-pie-chart',
          path: '/layout/chart',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'user',
        component: User,
        meta: {
          title: '用户列表',
          icon: 'el-icon-user',
          path: '/layout/user',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'enterprise',
        component: Enterprise,
        meta: {
          title: '企业列表',
          icon: 'el-icon-office-building',
          path: '/layout/enterprise',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'question',
        component: Question,
        meta: {
          title: '题库列表',
          icon: 'el-icon-edit-outline',
          path: '/layout/question',
          roles: ['超级管理员', '管理员', '老师', '学生']
        }
      },
      {
        path: 'subject',
        component: Subject,
        meta: {
          title: '学科列表',
          icon: 'el-icon-notebook-2',
          path: '/layout/subject',
          roles: ['超级管理员', '管理员', '老师']
        }
      }
    ]
  },
  // 测试用的
  {
    path: '/parent',
    component: Parent
  },
  {
    path: '/slot',
    component: Slot
  },
  {
    path: '/slot3',
    component: Slot3
  }
]
const router = new VueRouter({
  routes
})

// 解决路由重复报错的问题
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push (location) {
//   return originalPush.call(this, location).catch(err => err)
// }

/**
 * beforeEach  导航首位
 *
 * to 去往何处
 * from从何而来
 * next 控制去不去或者去到哪里，并且必须要调用，否则就会一直卡在这儿
 * next()如果里面不加参数,就是正常的跳转到下一个页面
 * next 如果里面加了参数,就是跳转到指定的页面
 *
 * */

// 访问的白名单, 可以直接通行
const whiteList = ['/login', '/parent', '/slot', '/slot3']
router.beforeEach((to, from, next) => {
  //访问的路径再白名单中
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (getToken()) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(to => {
  document.title = to.meta.title || '黑马面面'
})
export default router
